package com.cskaoyan.wx.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.*;
import com.cskaoyan.utils.UserUtils;
import com.cskaoyan.wx.vo.GoodsDetailVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.lang.System;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    GoodsAttributeMapper goodsAttributeMapper;
    @Autowired
    BrandMapper brandMapper;
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    IssueMapper issueMapper;
    @Autowired
    GoodsProductMapper goodsProductMapper;
    @Autowired
    GoodsSpecificationMapper goodsSpecificationMapper;
    @Autowired
    UserCollectMapper userCollectMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    FootPrintMapper footPrintMapper;

    @Override
    public int getGoodsCount() {

        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria goodCriteria = goodsExample.createCriteria();
        goodCriteria.andIsOnSaleEqualTo(true);
        goodCriteria.andDeletedEqualTo(false);

        long l = goodsMapper.countByExample(goodsExample);

        return (int) l;
    }

    @Override
    public GoodsDetailVO getDetail(int id) {
        //记录足迹
        Integer userId = UserUtils.getUserId();
        if (userId != null) {
            FootPrint footPrint = new FootPrint();
            footPrint.setId(null);
            footPrint.setUserId(userId);
            footPrint.setGoodsId(id);
            footPrint.setAddTime(new Date());
            footPrint.setUpdateTime(null);
            footPrint.setDeleted(false);
            footPrintMapper.insert(footPrint);
        }

        GoodsDetailVO detailVO = new GoodsDetailVO();

        Goods goods = goodsMapper.selectByPrimaryKey(id);
        detailVO.setInfo(goods);

        GoodsAttributeExample goodsAttributeExample = new GoodsAttributeExample();
        GoodsAttributeExample.Criteria attributeCriteria = goodsAttributeExample.createCriteria();
        attributeCriteria.andGoodsIdEqualTo(id);
        List<GoodsAttribute> goodsAttributes = goodsAttributeMapper.selectByExample(goodsAttributeExample);
        detailVO.setAttribute(goodsAttributes);

        Brand brand = brandMapper.selectByPrimaryKey(goods.getBrandId());
        detailVO.setBrand(brand);

        PageHelper.startPage(1, 3);
        CommentExample commentExample = new CommentExample();
        commentExample.setOrderByClause("add_time desc");
        CommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andValueIdEqualTo(id);
        List<Comment> comments = commentMapper.selectByExample(commentExample);

        PageInfo<Comment> pageInfo = new PageInfo<>(comments);
        detailVO.setComment(new GoodsDetailVO.CommentVO((int) pageInfo.getTotal(), comments));
        PageHelper.clearPage();

        List<Issue> issues = issueMapper.selectByExample(new IssueExample());
        detailVO.setIssue(issues);

        GoodsProductExample goodsProductExample = new GoodsProductExample();
        GoodsProductExample.Criteria productCriteria = goodsProductExample.createCriteria();
        productCriteria.andGoodsIdEqualTo(id);
        List<GoodsProduct> goodsProducts = goodsProductMapper.selectByExample(goodsProductExample);
        detailVO.setProductList(goodsProducts);

        GoodsSpecificationExample goodsSpecificationExample = new GoodsSpecificationExample();
        GoodsSpecificationExample.Criteria specificationCriteria = goodsSpecificationExample.createCriteria();
        specificationCriteria.andGoodsIdEqualTo(id);
        List<GoodsSpecification> goodsSpecifications = goodsSpecificationMapper.selectByExample(goodsSpecificationExample);
        detailVO.setSpecificationList(goodsSpecifications);
        //System.out.println(goodsSpecifications);

        UserCollectExample userCollectExample = new UserCollectExample();
        UserCollectExample.Criteria collectExampleCriteria = userCollectExample.createCriteria();
        collectExampleCriteria.andValueIdEqualTo(id);
        long collectCount = userCollectMapper.countByExample(userCollectExample);

        detailVO.setUserHasCollect((int) collectCount);

        return detailVO;
    }

    @Override
    public List<Goods> getRelated(int id) {

        // 随机返回热门商品
        PageHelper.startPage(new Random().nextInt(2) + 1, new Random().nextInt(3)+1);
        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria goodCriteria = goodsExample.createCriteria();
        goodCriteria.andIsHotEqualTo(true);
        goodCriteria.andIsOnSaleEqualTo(true);
        goodCriteria.andDeletedEqualTo(false);
        List<Goods> hotGoods = goodsMapper.selectByExample(goodsExample);
        return hotGoods;
    }
}
